<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form
          class="login_form"
          :model="loginForm"
          :rules="rules"
          ref="loginForms"
        >
          <h1>Hello</h1>
          <h2>欢迎来到硅谷甄选</h2>
          <el-form-item label="用户" prop="username">
            <el-input
              :prefix-icon="User"
              v-model="loginForm.username"
            ></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input
              type="password"
              :prefix-icon="Lock"
              v-model="loginForm.password"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              size="default"
              class="login_btn"
              @click="login"
              :loading="loading"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { ElMessage, ElNotification } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import { ref, reactive, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
// 引入获取当前时间的函数
import { getTime } from '@/utils/time'
// 引入用户相关的小仓库
import useUserStore from '@/store/modules/user'
const useStore = useUserStore()

// 获取el-form 组件， 进行验证使用
const loginForms = ref()

// 获取路由器
const $router = useRouter()
// 获取路由duixiang
const $route = useRoute()

const loading = ref(false)

// 收集账号与密码的数据
const loginForm = reactive({
  username: 'admin',
  password: 'atguigu123',
})
const login = async () => {
  // console.log(123)
  // 保证全部表单的校验规则通过再发请求
  await loginForms.value.validate()

  loading.value = true
  try {
    await useStore.userLogin(loginForm)
    // 登录成功，编程式导航跳转到登录的首页
    // 登录时进行判断，有query参数， 跳转到query参数的页面，没有则跳转到首页
    const redirect: any = $route.query.redirect
    $router.push({ path: redirect || '/' })
    // if (redirect) {
    //   $router.push(redirect)
    // } else {
    //   $router.push('/')
    // }
    // 登录成功的提示信息
    ElNotification({
      type: 'success',
      message: '欢迎回来',
      title: `HI,${getTime()}好`,
    })
    // loading.value = false
  } catch (error) {
    // loading.value = false
    ElNotification({
      type: 'error',
      message: (error as Error).message,
    })
  } finally {
    loading.value = false
  }
}
onMounted(() => {
  // login()
})

// 自定义校验规则的函数
const validateUserName = (rule: any, value: any, callback: any) => {
  if (value.length >= 5) {
    callback()
  } else {
    callback(new Error('账号长度为5-10位'))
  }
}

const validatePassword = (rule: any, value: any, callback: any) => {
  if (value.length >= 6) {
    callback()
  } else {
    callback(new Error('密码长度为6-15位'))
  }
}

// 输入校验 rules
const rules = {
  username: [
    // { required: true, message: '用户名不能为空', trigger: 'blur' },
    // {
    //   required: true,
    //   min: 4,
    //   max: 8,
    //   message: '账号长度为4-8位',
    //   trigger: 'blur',
    // },
    { trigger: 'blur', validator: validateUserName },
  ],
  password: [
    // {
    //   required: true,
    //   min: 6,
    //   max: 15,
    //   message: '账号长度为6-15位',
    //   trigger: 'blur',
    // },
    { trigger: 'blur', validator: validatePassword },
  ],
}
</script>

<style scoped lang="scss">
.login_container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;
  .login_form {
    position: relative;
    width: 80%;
    top: 30vh;
    background: url('@/assets/images/login_form.png') no-repeat;
    background-size: cover;
    padding: 40px;
    h1 {
      color: #fff;
      font-size: 40px;
    }
    h2 {
      color: #fff;
      font-size: 20px;
      margin: 20px 0;
    }
    ::v-deep .el-form-item__label {
      color: #fff;
    }
    .login_btn {
      width: 90%;
      margin: 0px auto;
      margin-top: 20px;
    }
  }
}
</style>
